package com.xuelang.pipeline.app.resource.request;

import com.alibaba.fastjson2.JSON;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.pipeline.client.common.util.UserContext;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * ngac 权限管理
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class NgacAssociateXO {

    /**
     * 权限ID
     */
    private String id;

    /**
     * 资源类型
     */
    private String objectType;
    /**
     * 资源ID
     */
    private String objectId;
    /**
     * 父级资源ID
     */
    private String objectParentId;

    private String relation;

    /**
     * 授权对象ID
     */
    private String targetUser;

    /**
     * 授权对象属性
     */
    private String userAttribute;

    /**
     * 访问策略
     */
    private String policyClass;

    private String operationSet;

    private String conditionId;

    private Date createTime;

    private String createBy;


    private Date updateTime;

    private String updateBy;


    private Integer delFlag;


    public static void main(String[] args) {
        NgacAssociateXO build = NgacAssociateXO.builder()
                .objectParentId("componentDTO.getApplicationCode()")
                .objectId("componentDTO.getLabel()")
                .objectType(ObjectTypeEnum.component.name())
                .targetUser(TracingUtils.getUserId())
                .userAttribute(UserAttribute.builder()
                        .userName(UserContext.getUserName())
                        .role(RoleIdEnum.manager.name())
                        .roleName(RoleIdEnum.manager.getDescription())
                        .build()
                        .toString()
                )
                .build();
        System.out.println(build);

        NgacAssociateXO build1 = NgacAssociateXO.builder()
                .objectId("componentLabel")
                .objectParentId("component.getApplicationCode()")
                .objectType(ObjectTypeEnum.component.name())
                .targetUser(TracingUtils.getUserId())
                .build();
        System.out.println(build1);
    }

    /**
     * 用户属性
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserAttribute {
        /**
         * 用户名
         */
        private String userName;
        /**
         * 角色ID
         */
        private String role;
        /**
         * 角色名
         */
        private String roleName;

        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }

    /**
     * 资源类型
     */
    public enum ObjectTypeEnum {
        /**
         * 应用
         */
        app,
        /**
         * 组件
         */
        component,
        /**
         * 制品仓库
         */
        product;

    }

    public enum RoleIdEnum {
        /**
         * 管理员
         */
        manager("管理者"),
        /**
         * 开发人员
         */
        developer("开发人员"),
        /**
         * 普通用户
         */
        user("普通用户");

        private String description;

        RoleIdEnum(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
